POV-Ray : Newsgroups : povray.unofficial.patches : Tesselation and mesh data extraction patches : Tesselation and mesh data extraction patches Server Time
27 Apr 2024 13:42:15 EDT (-0400)
  Tesselation and mesh data extraction patches  
From: Warp
Date: 13 Dec 2000 15:35:13
Message: <3a37dd81@news.povray.org>
I have been playing with the two patches mentioned in the subject.

  I'll describe my ideas and I hope to get some suggestions about the syntax,
improvement ideas and so on.


  - The mesh data extraction patch
    ==============================

  This is an independent patch which can be used with any mesh. Although
it's useful in itself, it makes the tesselation patch to make sense
(the tesselation patch without this patch would be pretty useless).

  The idea is that you could get individual values (vertices, triangles,
normals...) from a mesh object.
  This can be used to, for example, export a mesh to an ascii file format
(such as PCM or other more popular mesh formats), use macros on the mesh
(such as the SSS or the PCM macros) and so on.

  Right now I have done these functions:

  * get_triangle_count(MESH)
      Returns the number of triangles in the mesh

  * get_vertex_count(MESH)
      Returns the number of vertices in the mesh

  * get_normal_count(MESH)
      Returns the number of normal vectors in the mesh

  * get_vertex(MESH, INDEX)
      Returns the vertex vector that corresponds to the given index
      value (starting from 0)

  * get_normal(MESH, INDEX)
      As get_vertex, but returns a normal vector

  * get_vertex_indices(MESH, INDEX)
      Returns a vector containing index values to vertices for the triangle
      number INDEX (starting from 0). This can be used to read the three
      vertices of the triangle calling get_vertex() with each vector component

  * get_normal_indices(MESH, INDEX)
      The same as get_vertex_indices(), but returns indices to the normal
      vectors of the triangle number INDEX

  * is_smooth_triangle(MESH, INDEX)
      Tells whether the triangle number INDEX is smooth or not (not yet
      implemented).

  As you can see, there are lots of functions.

  My first idea was to put everything into arrays so that you could just
do all that with one function, like for example:

  get_mesh_data(MESH, VERTICES, NORMALS, TRIANGLES)

(with the three last parameters being undefined identifiers).
  You could then get the count values by reading the size of the arrays and
the data would be inside the arrays.
  (Another option would be to create three functions, each one returning
one array.)

  This latter option certainly sounds better than having 8 functions to get
the triangle data.
  However, it has one drawback: It takes a lot of memory to create those
arrays. Studying the povray source I have deduced that the arrays would
take more than twice the memory than the mesh itself (which would mean
that it takes more than three times the memory required for the mesh
itself to be able to handle the mesh and the arrays).

  But I can add this kind of function as an option. It's not like it was
mutually exclusive with the 8 first functions. Both could exist.

  Here is an example macro that writes a mesh to PCM format:

#macro ExportToPCM(Mesh, FileName)
  #fopen OutFile FileName write
  #local vrt_cnt = get_vertex_count(Mesh);
  #local nrm_cnt = get_normal_count(Mesh);
  #write(OutFile, "\"PCM1\",\n", vrt_cnt+nrm_cnt, ",\n")
  #local VInd = 0;
  #while(VInd < vrt_cnt)
    #local V = get_vertex(Mesh, VInd);
    #write(OutFile, V.x, ",", V.y, ",", V.z, ",")
    #local VInd = VInd+1;
  #end
  #local NInd = 0;
  #while(NInd < nrm_cnt)
    #local N = get_normal(Mesh, NInd);
    #write(OutFile, N.x, ",", N.y, ",", N.z, ",")
    #local NInd = NInd+1;
  #end
  #local tr_cnt = get_triangle_count(Mesh);
  #write(OutFile, "\n0,", tr_cnt, ",\n")
  #local TInd = 0;
  #while(TInd < tr_cnt)
    #local Ind1 = get_vertex_indices(Mesh, TInd);
    #local Ind2 = vrt_cnt + get_normal_indices(Mesh, TInd);
    #write(OutFile,
      Ind1.x, ",", Ind2.x, ",",
      Ind1.y, ",", Ind2.y, ",",
      Ind1.z, ",", Ind2.z, ",")
    #local TInd = TInd+1;
  #end
  #fclose OutFile
#end


  - The tesselation patch
    =====================

  The tesselation patch defines a function called tesselate() which can
be used to convert any object with a finite bounding box into a triangle
mesh. The syntax is the following:

  tesselate(OBJECT, RESOLUTION, SMOOTH, BBOX_OFFSET)

  The two last parameters are optional.
  The function returns a regular mesh object.

  The parameters have to following meaning:

  OBJECT: Any object with a finite bounding box.

  RESOLUTION: An integer value which must be greater than 0 and which
    affects the accuracy of the tesselation. A larger value gives a higher
    accuracy.

  SMOOTH: Boolean value which tells the function whether it should calculate
    flat or smooth triangles. If smooth triangles are calculated, the normal
    vectors are taken from the object itself. The default value is false.

  BBOX_OFFSET: If this value is different from 0, then the dimensions taken
    from the bounding box are enlarged by this amount. Only very small
    values should be used if any (like 0.0001). The default value is 0.
      This operation is necessary to tesselate objects like box { -1,1 }.
    However, this is not usually necessary and using a value different
    from 0 can cause inaccuracies with some objects (if an edge of the
    object touches the bounding box).

  To do: To study whether it's possible to use some supersampling algorithm
to calculate more triangles at sharp edges of the object.
  Currently the tesselation is not very good with sharp edges.

-- 
main(i,_){for(_?--i,main(i+2,"FhhQHFIJD|FQTITFN]zRFHhhTBFHhhTBFysdB"[i]
):_;i&&_>1;printf("%s",_-70?_&1?"[]":" ":(_=0,"\n")),_/=2);} /*- Warp -*/


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.